#include "../inc/fdwrapper.h"

int set_socket_nonblock(int fd) {
    int flag;
    flag = fcntl(fd, F_GETFL, NULL);
    if (flag < 0) {
        return -1;
    } else {
		flag |= O_NONBLOCK;
	}
       
    if (fcntl(fd, F_SETFL, flag) < 0) {
        return -1;
    }
    
    return 0;
}

int set_socket_linger(int fd) {
    struct linger linger;
    
    linger.l_onoff = 1;
    linger.l_linger = 0;
    
    return setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(struct linger));
}

int set_socket_reusable( int fd ) {
    int reuse_on = 1;
    return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_on, sizeof(reuse_on));
}

int set_socket_keepalive( int fd ) {
	int flags = 1;
	return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&flags, sizeof(flags));
}

int set_socket_nodelay(int fd) {
	int flags = 1;
	return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&flags, sizeof(flags));
}